﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace SocketClients
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, int.Parse(textBox1.Text));
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            sock.Bind(ipEnd);
            sock.Listen(100);
            Socket clientSock = sock.Accept();

            byte[] Byte = new byte[4];
            clientSock.Receive(Byte);
            int length = Byte[0];
            string data = "";
            for (int i = 0; i < length; i++)
            {
                clientSock.Receive(Byte);
                for (int j = 0; j < Byte.Length; j++)
                    data += Byte[j];
            }
            textBox2.Text = data;
            clientSock.Close();
            sock.Close();
        }
    }
}
